package com.hyts.codegen.common.load.prop;

import com.hyts.codegen.common.base.GenModel;
import com.hyts.codegen.common.base.JdbcModel;
import com.hyts.codegen.common.config.ProcessGenConfig;
import com.hyts.codegen.common.load.JdbcGenLoadGenerator;
import com.hyts.codegen.common.model.JdbcGenModel;
import com.hyts.guava.object.Objects;
import com.hyts.properties.manager.PropertyManager;
import lombok.extern.slf4j.Slf4j;

/**
 * project-name:coffice-toolbox
 * package-name:com.hyts.codegen.process.load.prop
 * author:Libo/Alex
 * create-date:2019-06-18 13:18
 * email:liboware@gmail.com
 * description:此类主要用于: 读取配置文件的load加载器服务创建generator对象
 */
@Slf4j
public class PropertyGenLoadGenerator extends JdbcGenLoadGenerator {


    @Override
    public JdbcGenModel loadConfig(JdbcGenModel param) {
        if(null == param)
            return loadConfig();
        return param ;
    }

    @Override
    public JdbcGenModel loadConfig() {
        PropertyManager propertyManager = PropertyManager.getInstance().settingDefaultConfigurationName(ProcessGenConfig.DEFAULT_JDBC_GEN_PROPERTIES_FILE_NAME);
        JdbcModel jdbcModel = new JdbcModel();
        String ip = propertyManager.getValue2Text(ProcessGenConfig.DEFAULT_JDBC_GEN_PROPERTIES_JDBC_IP_KEY);
        jdbcModel.setIp(ip);
        Integer port = propertyManager.getValue2Number(ProcessGenConfig.DEFAULT_JDBC_GEN_PROPERTIES_JDBC_PORT_KEY);
        jdbcModel.setPort(port);
        String dbtype = propertyManager.getValue2Text(ProcessGenConfig.DEFAULT_JDBC_GEN_PROPERTIES_JDBC_DBTYPE_KEY);
        jdbcModel.setDbType(dbtype);
        String userName = propertyManager.getValue2Text(ProcessGenConfig.DEFAULT_JDBC_GEN_PROPERTIES_JDBC_USERNAME_KEY);
        jdbcModel.setUserName(userName);
        String password = propertyManager.getValue2Text(ProcessGenConfig.DEFAULT_JDBC_GEN_PROPERTIES_JDBC_PASSWORD_KEY);
        jdbcModel.setPassword(password);
        String database = propertyManager.getValue2Text(ProcessGenConfig.DEFAULT_JDBC_GEN_PROPERTIES_DATABASE_KEY);
        jdbcModel.setDatabase(database);
        log.info("jdbc连接信息:{}",Objects.toString(jdbcModel));
        GenModel genModel = new GenModel();
        genModel.setAuthor(propertyManager.getValue2Text(ProcessGenConfig.DEFAULT_GEN_PROPERTIES_AUTH_KEY));
        //genModel.setDatabaseName(propertyManager.getValue2Text(ProcessGenConfig).split(","));
        genModel.setPackageName(propertyManager.getValue2Text(ProcessGenConfig.DEFAULT_GEN_PROPERTIES_PACKAGE_KEY).split(","));
        return JdbcGenModel.builder().jdbcModel(jdbcModel).genModel(genModel).build();
    }
}
